package com.yeshine.opendata.service.impl;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IFavoritesDao;
import com.yeshine.opendata.entity.Favorites;
import com.yeshine.opendata.service.IFavoritesService;
import com.yeshine.opendata.utils.FileUtils;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.UploadConstantPath;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by taohj on 2014/7/15.
 */
@Service
public class FavoritesServiceImpl extends ServiceBase<Favorites> implements IFavoritesService {

	  private final String path= UploadConstantPath.ORG_HEADIMAGE_PATH;
    private String PATH = UploadConstantPath.GROUP_HEADIMAGE_PATH;



    private IFavoritesDao dao;

    @Autowired
    public void setDao(IFavoritesDao dao) {
        this.dao = dao;
    }


    @Override
    public Result<Favorites> create(Favorites favorites, HttpServletRequest request) {
        String path;
        String fileName="";
        Result<Favorites> result;
        MultipartFile file=favorites.getHeadImageFile();
        result=this.create(favorites);
        
        if (!result.isSuccess()){
            return result;
        }
        if (file!=null && file.getSize()>0){


            path = request.getSession().getServletContext().getRealPath(this.path);
            fileName = favorites.getId()+"_"+file.getOriginalFilename();
            File target = new File(path, fileName);
            try {
                FileUtils.copy(file.getInputStream(), target);
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<Favorites>(false,"复制文件失败");
            }
            favorites.setHeadImage(this.path+fileName);
            System.out.println(favorites.getHeadImage());
        }
        result=this.modify(favorites);
        return result;
    }

    @Override
    public Result<Favorites> remove(Favorites group) {
        return new Result<Favorites>(dao.delete(group) > 0);
    }


    @Override
    public Result<Favorites> modify(Favorites favorites,HttpServletRequest request) {
    	  String path;
          String fileName="";
          MultipartFile file=favorites.getHeadImageFile();
          if (file!=null && file.getSize()>0){


              path = request.getSession().getServletContext().getRealPath(this.path);
              fileName = favorites.getId()+"_"+file.getOriginalFilename();
              File target = new File(path, fileName);
              try {
                  FileUtils.copy(file.getInputStream(), target);
              } catch (IOException e) {
                  e.printStackTrace();
                  return new Result<Favorites>(false,"复制文件失败");
              }
              //favorites.setHeadimage(this.path+fileName);
              System.out.println(favorites.getHeadImage());
          }   
    	Favorites f = this.getById(favorites.getId()).getTarget();
    	 
        
        f.setName(favorites.getName());
        f.setHeadImage(favorites.getHeadImage()); 
        f.setDescription(favorites.getDescription());
        f.setHeadImage(this.path+fileName);
        return new Result<Favorites>(dao.update(f) > 0);
    }


    @Override
    public Result<Favorites> getById(Integer id) {
        return new Result<Favorites>(dao.selectById(id));
    }

    @Override
    public Result<Favorites> getList(Favorites favorites) {
        return new Result<Favorites>(dao.selectList(favorites));
    }

    @Override
    public Result<Favorites> getByPage(PageResult<Favorites> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<Favorites>(pageResult);
    }


	@Override
	public Result<Favorites> create(Favorites t) {
		
		return new Result<Favorites>(dao.insert(t)>0);
	}


	@Override
	public Result<Favorites> modify(Favorites t) {
    	Favorites f = this.getById(t.getId()).getTarget();
    	 
        
        f.setName(t.getName());
        f.setHeadImage(t.getHeadImage()); 
        f.setDescription(t.getDescription());
        
        return new Result<Favorites>(dao.update(f) > 0);
	}


}
